package com.heze.xunhuan.controller;

import com.heze.xunhuan.entity.Message;
import com.heze.xunhuan.service.MessageService;
import com.heze.xunhuan.utils.SecurityContextUtil;
import com.heze.xunhuan.vo.PageVO;
import com.heze.xunhuan.vo.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 消息控制器
 *
 * @author heze-xunhuan
 */
@Tag(name = "私信沟通", description = "用户私信相关接口")
@RestController
@RequestMapping("/api/v1/conversations")
@RequiredArgsConstructor
public class MessageController {

    private final MessageService messageService;

    @Operation(summary = "获取会话列表", description = "获取当前用户的会话列表")
    @GetMapping
    public Result<PageVO<Map<String, Object>>> getConversations(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer size) {
        Long userId = SecurityContextUtil.getCurrentUserId();
        if (userId == null) {
            return Result.unauthorized("未登录");
        }
        PageVO<Map<String, Object>> pageVO = messageService.getConversations(userId, page, size);
        return Result.success(pageVO);
    }

    @Operation(summary = "创建会话", description = "创建新的会话")
    @PostMapping
    public Result<Long> createConversation(@RequestBody Map<String, Object> params) {
        Long userId = SecurityContextUtil.getCurrentUserId();
        if (userId == null) {
            return Result.unauthorized("未登录");
        }

        // 参数校验
        if (params.get("targetUserId") == null) {
            return Result.badRequest("目标用户ID不能为空");
        }

        Long targetUserId;
        try {
            targetUserId = Long.parseLong(params.get("targetUserId").toString());
        } catch (NumberFormatException e) {
            return Result.badRequest("目标用户ID格式错误");
        }

        Long postId = params.get("postId") != null ? Long.parseLong(params.get("postId").toString()) : null;
        String firstMessage = params.get("firstMessage") != null ? params.get("firstMessage").toString() : null;

        Long conversationId = messageService.createConversation(userId, targetUserId, postId, firstMessage);
        return Result.success(conversationId);
    }

    @Operation(summary = "获取聊天记录", description = "获取指定会话的聊天记录")
    @GetMapping("/{conversationId}/messages")
    public Result<PageVO<Message>> getMessages(
            @PathVariable Long conversationId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer size) {
        Long userId = SecurityContextUtil.getCurrentUserId();
        if (userId == null) {
            return Result.unauthorized("未登录");
        }
        PageVO<Message> pageVO = messageService.getMessages(conversationId, userId, page, size);
        return Result.success(pageVO);
    }

    @Operation(summary = "发送消息", description = "发送一条新消息")
    @PostMapping("/{conversationId}/messages")
    public Result<Long> sendMessage(@PathVariable Long conversationId, @RequestBody Map<String, Object> params) {
        Long userId = SecurityContextUtil.getCurrentUserId();
        if (userId == null) {
            return Result.unauthorized("未登录");
        }
        // TODO: 实现发送消息逻辑
        return Result.success();
    }

    @Operation(summary = "标记消息已读", description = "标记会话中的消息为已读")
    @PutMapping("/{conversationId}/read")
    public Result<Void> markAsRead(@PathVariable Long conversationId) {
        Long userId = SecurityContextUtil.getCurrentUserId();
        if (userId == null) {
            return Result.unauthorized("未登录");
        }
        messageService.markAsRead(conversationId, userId);
        return Result.success();
    }
}

